#! /bin/bash
export LANG=en_US.UTF-8
cpumin=
cpumax=
load5=0

FLAGE=
StatStr=
CURNUM=				#当前的性能参数(剩余cpu)
USEDNUM=                        #已使用的cpu(包括sys+user用户)
WranResultStr=		#最后形成的告警字符串
ErrorResultStr=		#最后形成的错误字符串
DataResultStr=		#性能呢个参数字符串

quota=
QUOTASTR=

function gotErr(){
	if [ "$1" -eq 0 ];then
		echo "mailstatedes=各项指标恢复正常 statedes=各项指标恢复正常|$DataResultStr"
	elif [ "$1" -eq 1 ];then 
		echo "quota=${quota} mailstatedes=$WranResultStr statedes=$WranResultStr ${QUOTASTR} |$DataResultStr"
	elif [ "$1" -eq 2 ];then 
		if [[ "${WranResultStr}" != "" ]]; then
			echo "quota=${quota} mailstatedes=${WranResultStr},${ErrorResultStr} statedes=${WranResultStr},${ErrorResultStr} ${QUOTASTR}|$DataResultStr"
			else
			echo "quota=${quota} mailstatedes=${ErrorResultStr} statedes=${ErrorResultStr} ${QUOTASTR}|$DataResultStr"
		fi
		
	elif [ "$1" -eq 3 ];then 
		echo "mailstatedes=无法获取到指标,疑似组件故障,请确认并请检查监控脚本和运维平台配置 statedes=无法获取到指标,疑似组件故障,请确认并请检查监控脚本和运维平台配置"
	fi

	exit $1
}

function contain(){
	result=$(echo $1 | grep "$2")
	if [[ "$result" != "" ]]
	then
		return 1
	else
		return 0
	fi
}

#依据传入的参数值来判断我们要监控的服务是什么
#然后通过各个函数获取到具体的值
#值之间的
function analysisStat(){
#剩余cpu百分比(是一个数字)
#CURNUM=$(top -b -n 1 | grep Cpu | awk '{print $5}' | cut -f 1 -d "." )

	#USEDNUM=$(expr 100 '-' ${CURNUM})
	USEDNUM=$(mpstat 1 2 |grep "Average"|awk '{print $3}'|sed "s/\([0-9]*\)\..*/\1/g")
	USEDNUM=$(sar -P ALL 1 1 | grep 'Average' | grep -v CPU | head -n ${FLAGE} | tail -n 1 | awk '{print $3}')
	parseFileds "cpu" $cpumin $cpumax
	
}

#CURNUM			当前值
#WARNLIMIT 		告警的上限
#ERRORLIMIT 	异常的上限
#判断以上三个值并拼接结果字符串
function parseFileds(){
	val1=$(echo "$USEDNUM $2"| awk '{if($1<=$2){print 1}else{print 0}}')
	val2=$(echo "$USEDNUM $3"| awk '{if($1<=$2){print 1}else{print 0}}')

	DataResultStr="$DataResultStr $1=$USEDNUM;$2;$3;0;0"
	if [ $val1 -eq 1 ];then
		return 0
	elif [[ $val1 -eq 0 && $val2 -eq 1 ]];then
		if [[ ${WranResultStr} != "" ]]; then
			WranResultStr="${WranResultStr},"
		fi

		WranResultStr="${WranResultStr}$1当前使用率为${USEDNUM}%超过告警值${2}%"

		if [[ ${quota} != "" ]]; then
			quota="${quota},"
		fi

		quota="${quota}$1"

		QUOTASTR="${QUOTASTR}$1=$1当前使用率为${USEDNUM}%超过告警值${2}% "

		return 1
	else
		if [[ ${ErrorResultStr} != "" ]]; then
			ErrorResultStr="${ErrorResultStr},"
		fi
		ErrorResultStr="${ErrorResultStr}$1当前使用率为${USEDNUM}%超过紧急值${3}%"

		if [[ ${quota} != "" ]]; then
			quota="${quota},"
		fi

		quota="${quota}$1"

		QUOTASTR="${QUOTASTR}$1=$1当前使用率为${USEDNUM}%超过紧急值${3}% "
		
		return 2
	fi
}

#分析结果 给出给出状态
function analysisResult(){
	if [[ "$WranResultStr" == "" && "$ErrorResultStr" == "" ]];then
		gotErr 0
	elif [[ "$ErrorResultStr" == "" ]];then
		gotErr 1
	else
		gotErr 2
	fi
}

#输出错误信息并且退出程序
function showHelp(){
	if [ "$1" != "" ];then
		echo "请输入$1"
	fi
	echo "check_cpu.sh 可以监听本地的cpu使用情况的状态 参数如下"
	echo "check_cpu.sh [-w cpu] [-c cpu] -f <<FLAGE>>"
	echo "-w 表示监控警告的值"
	echo "-c 表示异常值"
	echo "-c 表示cpu的标识"
	exit 1
}

#检查参数是不是数字
function isNumber(){
	isNum=$(echo "$1" | awk '{print($0~/^([0-9])+[\.]?([0-9])?$/)?1:0}')
	return $isNum
}

function isLarge(){

	isNumber $1
	if [[ $? -eq 0 ]]; then
		return 1
	fi

	isNumber $2
	if [[ $? -eq 0 ]]; then
		return 1
	fi

	isLarge=$(echo "$1 $2"| awk '{if($1>=$2){print 1}else{print 0}}')
	if [ $isLarge -eq 1 ];then
		return 0
	else
		return 1
	fi
}

function checkARG(){
	if [[ "$2" != "" && "$3" != "" ]];then
		isLarge "$3" "$2"
		if [[ $? -eq 1 ]]; then
			return 1
		else
			$1=1
		fi
	fi
	return 0
}

#检查参数是否合法
#即参数是否是小数和整数
#对于守护进程的选择 这个值可以不验证
function checkALL(){

	if [[ "$cpumin" != "" || "$cpumax" != "" ]];then
		if [[ "$cpumax" == "" || "$cpumin" == "" ]]; then
			return 1
		fi

		isLarge "$cpumax" "$cpumin"
		if [[ $? -eq 1 ]]; then
			return 1
		else
			load5=1
		fi
	fi
	return 0
}



function parseARG(){
	#it mean warning
	if [ $1 -eq 1 ];then
 		cpumin=$(echo $2 | awk -F ',' '{print $1}' | sed "s/[0-9]\.[0-9]//g")
 	else
 		cpumax=$(echo $2 | awk -F ',' '{print $1}' | sed "s/[0-9]\.[0-9]//g")
 	fi

}

while getopts "w:c:f:h" arg
do
	case $arg in
		w)
			parseARG 1 "$OPTARG"
			;;
		c)
			parseARG 2 "$OPTARG"
			;;
	  f)
	    if [[ "$OPTARG" == "all" ]];then
	    	FLAGE=1;
	    else
			  FLAGE=$[OPTARG+2]
			fi
			;;		
		h)
			showHelp
			;;
		?) 
			showHelp
			;;
	esac
done

checkALL
if [[ $? == 1 ]];then
	showHelp "正确参数"
fi

#插件正题部分 
#确定存活的
#启动
#分析
analysisStat
analysisResult
